//
//  JGMicroPhoneViewController.m
//  JGMusicAnimation
//
//  Created by Mei Jigao on 16/3/9.
//  Copyright © 2016年 MEETStudio. All rights reserved.
//

#import "JGMicroPhoneViewController.h"
#import "JGMusicAnimationConfig.h"
#import "JGEZAudio.h"
#import "JGMusicAnimationViewFFT.h"
#import "JGMusicAnimationViewWaveform.h"

@interface JGMicroPhoneViewController () <JGEZMicrophoneDelegate> {
    
    BOOL stopDrawing;
}

@property (nonatomic, strong, readonly) JGEZMicrophone *audioMicro;
@property (nonatomic, weak) IBOutlet JGMusicAnimationViewFFT *animationViewNo;
@property (nonatomic, weak) IBOutlet JGMusicAnimationViewFFT *animationViewSwap;
@property (nonatomic, weak) IBOutlet JGMusicAnimationViewFFT *animationViewSin;

@end

@implementation JGMicroPhoneViewController

#pragma mark - init & dealloc
- (instancetype)init {
    
    self = [super initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
    
    JGMALog();
    [_animationViewNo removeDisplayTimer];
    [_animationViewSwap removeDisplayTimer];
    [_animationViewSin removeDisplayTimer];
}

#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    [self.view setBackgroundColor:[UIColor darkGrayColor]];
    
    NSError *error = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
    if (error) {
        
        JGMALog(@"%@", error);
        error = nil;
    }
    
    CGFloat gain = 1.f;
    [_animationViewNo setGain:gain];
    [_animationViewSwap setGain:gain];
    [_animationViewSin setGain:gain];
    
    [_animationViewNo setWithSin:NO];
    [_animationViewSwap setSinSwaponly:YES];
    
    _audioMicro = [JGEZMicrophone microphoneWithDelegate:self];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_audioMicro startFetchingAudio];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [_audioMicro stopFetchingAudio];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBAction
- (IBAction)stopOrBegin:(id)sender {
    
    stopDrawing = !stopDrawing;
    
    [_animationViewNo shouldDrawing:!stopDrawing];
    [_animationViewSwap shouldDrawing:!stopDrawing];
    [_animationViewSin shouldDrawing:!stopDrawing];
    
    [_animationViewNo clear];
    [_animationViewSwap clear];
    [_animationViewSin clear];
}

#pragma mark - EZMicrophoneDelegate
- (void)microphone:(JGEZMicrophone *)microphone hasAudioReceived:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels {
    
    if (stopDrawing) {
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [weakSelf.animationViewNo updateWithBuffer:buffer[0] bufferSize:bufferSize];
        [weakSelf.animationViewSwap updateWithBuffer:buffer[0] bufferSize:bufferSize];
        [weakSelf.animationViewSin updateWithBuffer:buffer[0] bufferSize:bufferSize];
    });
}

#pragma mark - end

@end
